﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SetCameraSize : MonoBehaviour
{
    /// <summary>
    /// 当速度水平分量是向左时设置的相机视野大小
    /// </summary>
    public float LeftTargetSize;
    /// <summary>
    /// 当速度水平分量是向右时设置的相机视野大小
    /// </summary>
    public float RightTargetSize;

    public float SmoothTime = 0.4f;

    IEnumerator SmoothSetSize(Collider2D collision)
    {
        float timer = 0f;
        float targetSize = Mathf.Sign(collision.attachedRigidbody.velocity.x) < 0f ? LeftTargetSize : RightTargetSize;
        while (timer < SmoothTime)
        {
            timer += Time.deltaTime;
            GameManager.Instance.MainCameraCtr.SetFollowValue(Mathf.Lerp(GameManager.Instance.MainCameraCtr.CurrentCameraSize, targetSize, timer * Time.deltaTime + Time.deltaTime * 2.5f));
            yield return new WaitForSeconds(Time.deltaTime);
        }
        GameManager.Instance.MainCameraCtr.SetFollowValue(targetSize);
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.CompareTag(Constants.Tag_Player))
            StartCoroutine(SmoothSetSize(collision));
    }
}
